package cz.none.bezbarier.web.security;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationTrustResolverImpl;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.expression.WebSecurityExpressionRoot;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import cz.none.bezbarier.web.service.UserService;

public class SecurityInterceptor extends HandlerInterceptorAdapter {

	@Autowired
	private UserService service;

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		if (modelAndView != null && !modelAndView.getViewName().startsWith("redirect:")) {
			FilterInvocation filterInvocation = new FilterInvocation(request, response, new FilterChain() {
				@Override
				public void doFilter(ServletRequest request, ServletResponse response) throws IOException,
						ServletException {
					throw new UnsupportedOperationException();
				}
			});

			Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
			WebSecurityExpressionRoot sec = new WebSecurityExpressionRoot(authentication, filterInvocation);
			sec.setTrustResolver(new AuthenticationTrustResolverImpl());
			modelAndView.getModel().put("sec", sec);

			modelAndView.getModel().put("user", service.getCurrentUser());

		}
	}

}
